package com.niit.controller;


import com.niit.apis.ApplicationFeignApi;
import com.niit.entity.Application;
import com.niit.entity.Job;
import com.niit.entity.User;
import com.niit.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/application")
public class ApplicationController {

    @Autowired
    private ApplicationFeignApi applicationFeignApi;

    //添加申请（求职者发起）
    @PostMapping("/add")
    public Result<Object> addApplication(@RequestBody Application application){
        try {
            System.out.println(application);
            applicationFeignApi.addApplication(application);
            return Result.success();
        }catch (Exception ignored){}
        return Result.error("已经申请过了");
    }

    //更新申请（求职者修改）
    @PutMapping("/update")
    public Result<Object> updateApplication(@RequestBody Application application){
       applicationFeignApi.updateApplication(application);
       return Result.success();
    }

    //删除申请（求职者与公司管理员删除）
    @DeleteMapping("/delete/{id}")
    public Result<Object> deleteApplication(@PathVariable("id") int id){
        applicationFeignApi.deleteApplication(id);
        return Result.success();
    }

    //获取所有申请
    @GetMapping("/getAll")
    public Result<Object> getApplications(){
        return Result.success(applicationFeignApi.getApplications());
    }

    //获取某个申请
    @GetMapping("/get/{id}")
    public Result<Object> getApplicationById(@PathVariable("id") int id){
        return Result.success(applicationFeignApi.getApplicationById(id));
    }

    //获取某个求职者的申请
    @GetMapping("/getBySeeker")
    public Result<Object> getSeekerApplications(@RequestBody User seeker){
        return Result.success(applicationFeignApi.getSeekerApplications(seeker));
    }

    //获取某个职位的申请
    @GetMapping("/getByJob")
    public Result<Object> getJobApplications(@RequestBody Job job) {
        return Result.success(applicationFeignApi.getJobApplications(job));
    }
    //根据工作的发布者获取所有的职位申请
    @GetMapping("/getByCompany")
    public  Result<Object> getCompanyApplications(@RequestBody User user) {
        return Result.success(applicationFeignApi.getCompanyApplications(user));
    }

    //获取某个职位的申请数量
    @GetMapping("/getCountByJob")
    public Result<Object> getCountByJob(@RequestBody Job job) {
        return Result.success(applicationFeignApi.getCountByJob(job));
    }

//    @GetMapping("/resume/download/{resumeHash}")
//    public ResponseEntity<Resource> downloadResume(
//            @PathVariable String resumeHash,
//            @RequestParam(required = false) String filename) {
//
//        // 伪代码：实际需根据您的存储实现
//        Resource resource = storageService.load(resumeHash);
//
//        return ResponseEntity.ok()
//                .header(HttpHeaders.CONTENT_DISPOSITION,
//                        "attachment; filename=\"" + (filename != null ? filename : "resume") + "\"")
//                .body(resource);
//    }
}
